/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * DataOperations class is used to perform database operations.
 * @author vincent
 */
public class DataOperations {
    
    /** Creates a new instance of DataOperations */
    public DataOperations() {
        con = this.getConnection();
        try {
            st = con.createStatement();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
   
    
    private String getConnectionUrl() {
        
        url = "jdbc:sqlserver://";
        serverName = "localhost";
        databaseName = "SweetBank";
        instanceName = "";
        userName = "sa";
        password = "fat123456";
        
        return url + serverName + 
                ";databaseName=" + databaseName;
    }
    
    /**
     * Returns the connection to the SQL Server 2005 database.
     * @return Returns the connection object
     */
    public Connection getConnection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(getConnectionUrl(),
                    userName, password);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        
        if(con != null)
            return con;
        
        return null;
    }
    
    /**
     * Instance variable to hold a reference to a Connection object.
     */
    private Connection con;
    
    /**
     * Instance variable to hold a reference to the Statement object.
     */
    private Statement st;
    
    /**
     * Instance variable to store the URL connection string.
     */
    private String url;
    
    /**
     * Instance variable to store the IP address of the machine
     * on which SQL Server 2005 database is located.
     */
    private String serverName;
    
    /**
     * Instance variable to store the name of the database to connect to student.
     */
    private String databaseName;
    
    /**
     * Instance variable to store the instance name of the SQL Server.
     */
    private String instanceName;
    
    /**
     * Instance variable to store the user name to connect to SQL Server.
     */
    private String userName;
    
    /**
     * Instance variable to store the password to connect to SQL Server.
     */
    private String password;
    
    /**
     * Instance variable to store the SQL query.
     */
    private String sql;
    
}
